package com.example.carrierapp.ui.login.forget;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.base.MVPBaseActivity;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.EditTextUtils;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetActivity extends MVPBaseActivity<ForgetContract.View, ForgetPresenter> implements ForgetContract.View {
    @BindView(R.id.et_login_number)
    EditText mEtNumber;
    @BindView(R.id.et_login_password)
    EditText mEtCode;

    @BindView(R.id.iv_login_password_clear)
    ImageView mIbCodeClear;
    @BindView(R.id.iv_login_number_clear)
    ImageView mIbNumberClear;

    @BindView(R.id.tv_login_code)
    TextView mTvCode;

    private String mNumber;
    private String mCode;
    private boolean m2Main;
    @Override
    protected ForgetPresenter createPresenter() {
        return new ForgetPresenter();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_forget;
    }

    @Override
    public String getTitleString() {
        return null;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        m2Main = getIntent().getBooleanExtra(ConstValue.LOGIN_2MAIN,true);
    }

    @Override
    public void initEvent() {
        super.initEvent();
        mEtNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phone = mEtNumber.getText().toString().trim();
                if (!TextUtils.isEmpty(phone)) {
                    mIbNumberClear.setVisibility(View.VISIBLE);
                } else {
                    mIbNumberClear.setVisibility(View.GONE);
                }


                mNumber = phone;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mEtCode.getText().toString().trim())) {
                    mIbCodeClear.setVisibility(View.VISIBLE);
                } else {
                    mIbCodeClear.setVisibility(View.GONE);
                }
                mCode = mEtCode.getText().toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private boolean checkMobil() {
        if (TextUtils.isEmpty(mNumber)) {
            return false;
        }

        if (mNumber.length() == 11) {
            if (EditTextUtils.checkMobile(mNumber)) {

                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.tv_login_code, R.id.tv_login_login, R.id.iv_login_password_clear, R.id.iv_login_number_clear})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_code:
                if (checkMobil()) {
                    mPresenter.sendCode(mNumber);
                    mVerificationTimer.start();
                } else {
                    ToastUtils.showCenterToast(this, "请输入正确的11位手机号码！");
                }
                break;
            case R.id.tv_login_login:
                if (TextUtils.isEmpty(mNumber)) {
                    ToastUtils.showCenterToast(this, "请输入手机号码！");
                } else if (TextUtils.isEmpty(mCode)) {
                    ToastUtils.showCenterToast(this, "请输入验证码！");
                } else {
                    showLoading("登录中。。。");
                    mPresenter.login(mNumber, mCode);
                }
                break;
            case R.id.iv_login_password_clear:
                mEtCode.setText("");
                break;
            case R.id.iv_login_number_clear:
                mEtNumber.setText("");
                break;
        }
    }

    private CountDownTimer mVerificationTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTvCode.setEnabled(false);
            mTvCode.setTextColor(Color.parseColor("#999999"));
            mTvCode.setText(getString(R.string.account_get_verification_code_cd, millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mTvCode.setEnabled(true);
            mTvCode.setTextColor(Color.parseColor("#2ecc71"));
            mTvCode.setText(getString(R.string.tv_resend_code));
        }
    };

    @Override
    public void loginSuccess(String msg) {
        dismissProgress();
        if(m2Main)
        {
            ActivityUtil.startForget(this,m2Main);
        }
        finish();
        ToastUtils.showCenterToast(this, "登录成功");
    }

    @Override
    public void loginFailde(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void sendCodeSuccess(String msg) {
        ToastUtils.showCenterToast(this, "发送成功");

    }

    @Override
    public void sendCodeFailde(String msg) {
        ToastUtils.showCenterToast(this, msg);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVerificationTimer.cancel();
    }
}
